import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:xinxuan/common/dialogs/del_dialog.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:xinxuan/http/http_util_service.dart';
import 'package:xinxuan/router/app_routers.dart';
import 'package:xinxuan/vo/blindbox/blindbox_order_detail_vo.dart';
import 'package:xinxuan/vo/blindbox/blindbox_order_list_vo.dart';
import 'package:xinxuan/vo/common_vo/common_pagination_vo.dart';

class SeckillOrderPageController extends GetxController {

  final CommonPageController commonPageController = Get.find();
  
  /// tab 下标
  var tabIndex = 0.obs;
  List<int> statusArr = [-1, 2, 4, 5, 8];
  /// 订单数据
  var dataArr = <BlindBoxOrderListVo>[
    BlindBoxOrderListVo(data: [], meta: BlindBoxOrderMeta(pagination: CommonPagination(total: 0, currentPage: 1))),
    BlindBoxOrderListVo(data: [], meta: BlindBoxOrderMeta(pagination: CommonPagination(total: 0, currentPage: 1))),
    BlindBoxOrderListVo(data: [], meta: BlindBoxOrderMeta(pagination: CommonPagination(total: 0, currentPage: 1))),
    BlindBoxOrderListVo(data: [], meta: BlindBoxOrderMeta(pagination: CommonPagination(total: 0, currentPage: 1))),
    BlindBoxOrderListVo(data: [], meta: BlindBoxOrderMeta(pagination: CommonPagination(total: 0, currentPage: 1))),
  ].obs;

  @override
  void onInit() {
    Map<String, dynamic> argumentsMap = Get.arguments;
    tabIndex.value = argumentsMap['tabIndex'];
    update();
    super.onInit();
  }

  @override
  void onReady() {
    getSeckillOrderList();
    super.onReady();
  }

  /// 获取订单列表
  getSeckillOrderList() async {
    // EasyLoading.show();
    // for (int i = 0; i < statusArr.length; i++) {
    //   BlindBoxOrderListVo tempVo = await HttpUtilService.getBlindBoxOrderList(status: statusArr[i], page: dataArr[i].meta!.pagination!.currentPage!, loading: false);
    //   dataArr[i] = tempVo;
    // }
    // EasyLoading.dismiss();

    // dataArr[tabIndex.value].meta!.pagination!.currentPage = 1;
    // BlindBoxOrderListVo tempVo = await HttpUtilService.getBlindBoxOrderList(status: statusArr[tabIndex.value], page: dataArr[tabIndex.value].meta!.pagination!.currentPage!, loading: true);
    // dataArr[tabIndex.value] = tempVo;
    // update();

    dataArr[tabIndex.value].meta!.pagination!.currentPage = 1;
    BlindBoxOrderListVo tempVo = await HttpUtilService.getBlindBoxOrderList(status: statusArr[tabIndex.value], page: dataArr[tabIndex.value].meta!.pagination!.currentPage!, loading: true);
    dataArr[tabIndex.value] = tempVo;
    update();
    
  }

  /// 加载更多订单列表
  onloadSeckillOrderList() async {    
    if (dataArr[tabIndex.value].meta!.pagination!.total! <= dataArr[tabIndex.value].data!.length) {
      return;
    }
    dataArr[tabIndex.value].meta!.pagination!.currentPage = dataArr[tabIndex.value].meta!.pagination!.currentPage! + 1;
    BlindBoxOrderListVo tempVo = await HttpUtilService.getBlindBoxOrderList(status: statusArr[tabIndex.value], page: dataArr[tabIndex.value].meta!.pagination!.currentPage!, loading: false);
    dataArr[tabIndex.value].data!.addAll(tempVo.data!);
    update();
  }

  /// 获取Tab某项订单列表
  getSeckillTabItemOrderList(int index) async {
    dataArr[index].meta!.pagination!.currentPage = 1;
    BlindBoxOrderListVo tempVo = await HttpUtilService.getBlindBoxOrderList(status: statusArr[index], page: dataArr[index].meta!.pagination!.currentPage!, loading: false);
    dataArr[index] = tempVo;
    update();
  }

  /// 取消订单
  Future<bool> closeSeckillOrder(int id) async {
    Get.dialog(DelDialog(message: '确认取消订单吗？', onTap: () async {
      var res = await HttpUtilService.closeSeckillOrder(id: id);
      if (res) {
        commonPageController.getUserInfo(loading: false);
        EasyLoading.showToast('取消成功');
        updateOrdersStatusClose(id);
      }
      return true;
    }));
    return false;
  }

  /// 去支付
  goPay(String payableAmount, String orderSn, int id, int status) {
    Get.toNamed(
      '${AppRouters.COMMON_PAGE}${AppRouters.SECKILL_ORDER_PAY_PAGE}',
      arguments: {
        'payAmount': payableAmount,
        'orderSn': orderSn
      }
    )!.then((res) {
      getOrderDetail(status, id);
    });
  }

  /// 查询某订单详情并本地修改数据
  getOrderDetail(int status, int id, {bool loading = true}) async {
    BlindBoxOrderDetailVo seckillOrderDetailVo = await HttpUtilService.getSeckillOrderDetail(orderId: id, loading: loading);
    if (seckillOrderDetailVo.data!.status == status) {
      return;
    }
    dataArr.forEach((element) {
      element.data!.forEach((item) {
        if (item.id == id) {
          item.status = seckillOrderDetailVo.data!.status;
          item.statusStr = seckillOrderDetailVo.data!.statusStr;
        }
      });
    });
    update();
  }

  /// 本地修改订单状态为已取消
  updateOrdersStatusClose(int id) {
    dataArr.forEach((element) {
      element.data!.forEach((item) {
        if (item.id == id) {
          item.status = 4;
          item.statusStr = '已取消';
        }
      });
    });
    update();
  }

}